package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"video-hub/models/mysql"

	"video-hub/videorpc/internal/svc"
	"video-hub/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoStatusLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoStatusLogic {
	return &VideoStatusLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 视频状态管理
func (l *VideoStatusLogic) VideoStatus(in *videorpc.VideoStatusRequest) (*videorpc.VideoStatusResponse, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.User
	err := findUser.FirstUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("该用户不存在")
	}
	var findVideo mysql.Video
	err = findVideo.FirstVideoById(mysql.DB, in.VideoId)
	if err != nil {
		return nil, errors.New("该视频不存在")
	}
	var findTopic mysql.Topic
	err = findTopic.FirstTopicById(mysql.DB, in.TopicId)
	if err != nil {
		return nil, errors.New("该话题不存在")
	}
	video := &mysql.Video{
		Model: gorm.Model{ID: uint(in.VideoId)},

		VideoTitle:    in.VideoTitle,
		VideoCategory: in.VideoCategory,
		VideoLink:     in.VideoLink,
		CoverLink:     in.CoverLink,
		VideoIntro:    in.VideoIntro,
		VideoStatus:   in.VideoStatus,
		TopicId:       in.TopicId,
	}
	err = video.UpdateVideo(mysql.DB)
	if err != nil {
		return nil, errors.New("操作失败")
	}
	return &videorpc.VideoStatusResponse{
		VideoId: int64(video.ID),
	}, nil
}
